/******************************************************************************************************
Copyright (C) 2008 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/ 

var MyPeakReporterSettingsKey = new String("MyPeakReporter/Custom Message");

/* This creates a Custom Peak Reporter  */
function MyPeakReporter()
{
	PeakReporter.call(this);
	
	var setts=new Settings(Application.settings);
	this.customMessage = setts.value( MyPeakReporterSettingsKey, "My Peak Reporter" );
	
	this.name = "My Peak Reporter";
	
// Define font size and font family
	this.font = "<font style=\"font-size: 10pt; font-family: Times New Roman\">"; 
		
	this.nucleusTemplate="%1";
// Report header. %1 will be raplaced with nucleusString, %2  with frequency, %3 with solvent 
	//this.header = "My Peak Reporter %1 NMR (%2 MHz, %3) &delta; ";
	this.header = this.customMessage+" %1 NMR (%2 MHz, %3) &delta; ";
//Peak templates:
	this.peakTemplate="%1";//%1 - delta
		
	this.deltaPrecision = 2; // delta precision
	this.pSeparator = ", "; // peak separator 
		
	this.start = "<html><head><title>Peak Report</title></head><body>" + this.font;
	this.end = ".</font></body></html>";
}

MyPeakReporter.prototype = new PeakReporter();
MyPeakReporter.prototype.toString = function() { return "MyPeakReporter()"; }

//This line adds my custom reporter to the list of reporters, this means that this reporter can be used by the functions like reportPeaksUser or automaticPeakReport and then it can be used to generate reports  from the Peaks Table
Env.PeakReportersArray.push(new MyPeakReporter());

//Uncomment this line to make this reporter the default reporter, the saved settings will be ignored, the user will not be able to modify the reporter from MNova
//Env.DefaultPeakReporter="My Peak Reporter";

//This function can be used to generate peak reports
function customReportPeaksUser(createItem)
{
	var peakTable = new PeakTable(nmr.peakTable()); // Get table of peaks
	var spectrum;
	if(peakTable.isValid())
		spectrum = new NMRSpectrum(peakTable.spectrum()); // Get spectrum currently shown in the table of peaks
	else
		spectrum = new NMRSpectrum(nmr.activeSpectrum());
	
	if( spectrum.isValid() )
	{
		var reporter = PeakReporter.getReporterByName("My Peak Reporter");
		return reporter.report(spectrum, createItem);
	}
	else
		return undefined;
}

//Uncomment this line to use the previous function to report multiplets instead of the default one
//Env.ReportPeaksUser = customReportPeaksUser;

//Function to setup my reporter
function customReportPeaksUserSetup()
{
	var rep = PeakReporter.getReporterByName("My Peak Reporter");
	
	var diag = new Dialog();
	diag.title = "Custom Setup";
	
	var lEdit = new LineEdit();
	lEdit.text = rep.customMessage;
	diag.add(lEdit);
	
	if(diag.exec())
	{
		rep.customMessage = lEdit.text;
		settings.setValue( MyPeakReporterSettingsKey, rep.customMessage );
	}
}

//Uncomment this line to use the previous function to setup the report peaks
//Env.ReportPeaksUserSetup = customReportPeaksUserSetup;
